home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13674 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  63 lines

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP : Assignment with strings and pointers.
  5. Date: 9 Apr 1996 11:34:03 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4keairINN3jv@mayne.ugrad.cs.ubc.ca>
  8. References: <316ac5b5.4283622@news.planet.net>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <316ac5b5.4283622@news.planet.net>,
  12. David  <cygnusx@planet.net> wrote:
  13. >
  14. >
  15. >This program is probably idiotic since I am in a (intro to C ) class
  16. >but it does work. I was wondering if anyone had a better idea for this
  17. >type of situation. I could not find an actual function in the
  18. >character handling library for this? does one exist ? i would like to
  19. >make a separate function in the main to handle this task? Is it
  20. >possible?
  21. >
  22. >thanx David
  23. >
  24. >
  25. >  #include <stdio.h>
  26. >  #include <ctype.h>
  27. >
  28. >   /* this program determines if the first word  is the second word
  29. >spelled backwords and have only included 3 characters for
  30. >simplification . inputs have been left out for clarity */
  31.  
  32.  
  33. You have to use loops to iterate over the characters of two strings
  34. simultaneously, forward over one and backward over the other. In other words,
  35. generalize the test you have hand-coded for the special case of three character
  36. strings.
  37.  
  38. First you trivially reject the case when the strings have a different length
  39. (or alternately perform the comparison on the first N characters of both
  40. strings, where N is the lesser of their two lengths).
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44.  
  45. int reversecmp(const char *s1, const char *s2)
  46.  
  47. {
  48.     size_t L1 = strlen(s1);
  49.     size_t L2 = strlen(s2);
  50.     size_t i, j;
  51.  
  52.     if (L1 != L2)
  53.         return 0;
  54.  
  55.     for (i = 0, j = L2-1; i < L1; i++, j--)
  56.         if (s1[i] != s2[j])
  57.             return 0;
  58.  
  59.     return 1;
  60. }
  61. -- 
  62.  
  63.